﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Service.Framework.Model;

namespace Service.Framework.AppStartup
{
    public class FrameworkApplication
    {
        public static async Task RunAsync<TStartupModule>(string[] args, Action<AppOptions>? options = null) where TStartupModule : IStartupModule
        {
            WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args);
            Console.WriteLine($"项目 {webApplicationBuilder.Environment.ApplicationName} 启动! -- {DateTime.Now:yyyy-MM-dd HH:mm:ss}\r\n\r\n");
            webApplicationBuilder.AddFramework<TStartupModule>(options);
            WebApplication webApplication = webApplicationBuilder.Build();
            webApplication.Use(async delegate (HttpContext context, RequestDelegate next)
            {
                await (next?.Invoke(context));
            });
            await webApplication.StartFrameworkAsync();
        }

        public static void Run<TStartupModule>(string[] args, Action<AppOptions>? options = null) where TStartupModule : IStartupModule
        {
            RunAsync<TStartupModule>(args, options).Wait();
        }
    }
}
